/*
 * Copyright (C) 2012 Tapas Mobile Ltd.  All Rights Reserved.
 */

package com.tools.cleanmaster.ui.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tools.cleanmaster.R;

import java.util.HashSet;


@SuppressWarnings("static-access")
public class PreferenceLayout extends LinearLayout {
    public interface OnPrefenceChangeListener {
        void onChange(PreferenceLayout pref, Object value);
    }

    protected static final boolean DEBUG = true;
    public static final String TYPE_NONE = "none";
    public static final String TYPE_NORMAL = "normal";
    public static final String TYPE_SWITCH = "switch";
    public static final String TYPE_TRIANGLE = "triangle";
    public static final String TYPE_ICON = "icon";

    private OnClickListener mOnClickListener = null;
    protected OnPrefenceChangeListener mOnPrefenceChangeListener = null;
    private Resources mRes;

    protected String mTitle = null, mNameString = null, mSummaryString = null;
    protected View mNormalPanel;
    protected TextView mName;
    protected ImageView mTextViewNewTip;
    protected TextView mSummary;
    protected View mTrianglePanel;
    protected TextView mTriangleName;
    protected TextView mTriangleSummary;
    protected ImageView mMore;
    protected ToggleButton mSwitch;
    protected ImageView mIcon;
    protected ImageView mDetailIcon;
    protected View mNewTip;
    protected String mValue = null;
    protected int mLayoutId;
    protected boolean mEnable;
    protected boolean mIsSwitch;
    protected boolean mIsNone;
    protected boolean mIsTriangle;
    protected boolean mIsIcon;
    protected String mType = null;
    protected boolean mIsNewTipShow;
    private PreferenceLayout mDependence;
    private HashSet<PreferenceLayout> mChilds = new HashSet<PreferenceLayout>();

    public PreferenceLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRes = getResources();
        setGravity(Gravity.CENTER_VERTICAL);
        setOrientation(HORIZONTAL);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PreferenceLayout);
        initLayoutId();
        if (mLayoutId == 0) {
            mLayoutId = a.getResourceId(R.styleable.PreferenceLayout_layout, R.layout.preference);
        }
        mTitle = a.getString(R.styleable.PreferenceLayout_title);
        mNameString = a.getString(R.styleable.PreferenceLayout_name);
        mSummaryString = a.getString(R.styleable.PreferenceLayout_summary);
        mEnable = a.getBoolean(R.styleable.PreferenceLayout_enabled, true);
        mType = a.getString(R.styleable.PreferenceLayout_uitype);
        mIsSwitch = TYPE_SWITCH.equals(mType);
        mIsTriangle = TYPE_TRIANGLE.equals(mType);
        mIsIcon = TYPE_ICON.equals(mType);
        mIsNone = TYPE_NONE.equals(mType);
        mIsNewTipShow = a.getBoolean(R.styleable.PreferenceLayout_newTipShow, false);
        a.recycle();
        inflate(getContext(), mLayoutId, this);
    }

    protected void initLayoutId() {
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mNormalPanel = findViewById(R.id.normal_panel);
        mTrianglePanel = findViewById(R.id.triangle_panel);

        mMore = findViewById(R.id.more);
        mName = findViewById(R.id.name);
        mTextViewNewTip = findViewById(R.id.dx_preference_new_tip_imageView);
        mIcon = findViewById(R.id.icon);
        mDetailIcon = findViewById(R.id.detail_icon);
        if (mNameString != null) {
            mName.setText(mNameString);
            mName.setVisibility(VISIBLE);
        }
        mTriangleName = findViewById(R.id.triangle_name);
        if (mTriangleName != null)
            mTriangleName.setText(mNameString);
        mSummary = findViewById(R.id.summary);
        if (!TextUtils.isEmpty(mSummaryString)) {
            setSummary(mSummaryString);
        } else {
            mSummary.setVisibility(View.GONE);
        }
        mTriangleSummary = findViewById(R.id.triangle_summary);
        if (mTriangleSummary != null)
            mTriangleSummary.setText(mSummaryString);
        mSwitch = findViewById(R.id.toggle);
        if (mIsSwitch) {
            mSwitch.setVisibility(View.VISIBLE);
            mMore.setVisibility(View.GONE);
            setChecked(isChecked());
        } else if (mIsNone) {
            mSwitch.setVisibility(View.GONE);
            mMore.setVisibility(View.GONE);
        } else if (mIsTriangle) {
            mSwitch.setVisibility(View.GONE);
            mMore.setVisibility(View.GONE);
            mNormalPanel.setVisibility(View.GONE);
            mTrianglePanel.setVisibility(View.VISIBLE);
        } else if (mIsIcon) {
            mIcon.setVisibility(View.VISIBLE);
            mDetailIcon.setVisibility(View.VISIBLE);
            mSwitch.setVisibility(View.GONE);
            mMore.setVisibility(View.VISIBLE);
        } else {
            mSwitch.setVisibility(View.GONE);
            mMore.setVisibility(View.VISIBLE);
        }

        mNewTip = findViewById(R.id.new_tip);
        if (mNewTip != null) {
            showNewTip(mIsNewTipShow);
        }

        this.setFocusable(true);
        this.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        updateEnabled();
        if (getBackground() == null) {
            this.setBackgroundResource(R.drawable.common_list_item_bkg);
        }
    }

    public void showTextViewNewTip(boolean show) {
        if (mTextViewNewTip != null) {
            if (show) {
                mTextViewNewTip.setVisibility(View.VISIBLE);
            } else {
                mTextViewNewTip.setVisibility(View.GONE);
            }
        }

    }

    public void setEnabled(boolean enable) {
        mEnable = enable;
        updateEnabled();
    }

    public void setDependence(PreferenceLayout pref) {
        if (pref == this)
            return;
        mDependence = pref;
        if (mDependence != null && mDependence.mIsSwitch) {
            mDependence.mChilds.add(this);
        }
    }

    private void updateEnabled() {
        mName.setEnabled(mEnable);
        if (mSummary != null)
            mSummary.setEnabled(mEnable);
        if (mMore != null)
            mMore.setEnabled(mEnable);
        if (mSwitch != null) {
            mSwitch.setEnabled(mEnable);
        }
        if (mSwitch != null) {
            mSwitch.setEnabled(mEnable);
        }
        if (mTriangleName != null)
            mTriangleName.setEnabled(mEnable);
        if (mTriangleSummary != null)
            mTriangleSummary.setEnabled(mEnable);
        this.setClickable(mEnable);
        this.setFocusable(mEnable);
    }

    @Override
    public void setOnClickListener(OnClickListener listener) {
        mOnClickListener = listener;
        // super.setOnClickListener(listener);
    }

    public void setChecked(boolean ischeck) {
        if (mSwitch != null) {
            mSwitch.setChecked(ischeck);
            for (PreferenceLayout pref : mChilds) {
                pref.setEnabled(ischeck);
            }
        }
    }

    public boolean isChecked() {
        if (mSwitch != null) {
            return mSwitch.isChecked();
        }
        return false;
    }

    public void setValue(String text) {
        mValue = text;
        mSummary.setText(text);
    }

    public void setSpanned(Spanned spanned) {
        mValue = spanned.toString();
        mName.setText(spanned);
    }

    public void setValue(int rid) {
        setValue(mRes.getString(rid));
    }

    public void setSummary(CharSequence spanned) {
        if (mSummary != null) {
            if (spanned != null && spanned.length() > 0) {
                mSummary.setVisibility(VISIBLE);
                mSummary.setText(spanned);
            } else {
                mSummary.setVisibility(GONE);
            }
        }
        if (mTriangleSummary != null)
            mTriangleSummary.setText(spanned);
    }

    public void setSummary(int sid) {
        if (mSummary != null) {
            mSummary.setVisibility(VISIBLE);
            mSummary.setText(sid);
        }
        if (mTriangleSummary != null)
            mTriangleSummary.setText(sid);
    }

    public void setTitle(int sid) {
        mTitle = mRes.getString(sid);
    }

    public void setIndicator(int resid) {
        mName.setCompoundDrawablesWithIntrinsicBounds(0, 0, resid, 0);
    }

    public void setName(CharSequence text) {
        mName.setText(text);
    }

    public void setName(int rid) {
        setName(mRes.getString(rid));
    }

    public void setNameColor(int color) {
        mName.setTextColor(color);
    }

    @Override
    public void setPressed(boolean pressed) {
        if (mMore != null)
            mMore.setPressed(pressed);
        super.setPressed(pressed);
    }

    public void setIcon(int resId) {
        mIcon.setImageResource(resId);
    }

    public void setDetailIcon(Drawable drawable) {
        if (drawable == null) {
            mDetailIcon.setVisibility(View.GONE);
        } else {
            mDetailIcon.setVisibility(View.VISIBLE);
            mDetailIcon.setImageDrawable(drawable);
        }
    }

    public void setDetailIcon(int resId) {
        if (resId <= 0) {
            mDetailIcon.setVisibility(View.GONE);
        } else {
            mDetailIcon.setVisibility(View.VISIBLE);
            mDetailIcon.setImageResource(resId);
        }
    }

    public void showNewTip(boolean isShow) {
        mNewTip.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public boolean performClick() {
        if (!mEnable)
            return false;
        if (DEBUG)
            Log.d(this.getClass().getSimpleName(), "doClick");

        if (mOnClickListener != null) {
            mOnClickListener.onClick(this);
        } else if (mIsSwitch) {
            boolean checked = !isChecked();
            setChecked(checked);
            if (mOnPrefenceChangeListener != null) {
                mOnPrefenceChangeListener.onChange(this, checked);
            }
        }
        return true;
    }

    public String getValue() {
        return mValue;
    }

    public CharSequence getText() {
        return mSummary.getText();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    public void setOnPrefenceChangeListener(OnPrefenceChangeListener listener) {
        mOnPrefenceChangeListener = listener;
    }


    public void setNameMarquee(){
        mName.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        mName.setSelected(true);
    }
}
